package com.fone.player.view;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.VgdetailRst;
import com.fone.player.client.XyzplaRst;
import com.fone.player.client.VgdetailRst.From;
import com.fone.player.util.ScreenUtil;

/**
 * 视频清晰度PopupWindow
 * 
 * @author kongxiaojun
 * @since 2014-5-26
 */
public class VideoDefinitionPopupWindow {

	private PopupWindow popupWindow;
	private ListView listView;
	private ItemClickCallBack callBack;
	private View contentView;
	private List<XyzplaRst.Dfnt> dfntList;

	public VideoDefinitionPopupWindow(Context mContext, List<XyzplaRst.Dfnt> dfntList, ItemClickCallBack callBack) {
		super();
		this.callBack = callBack;
		this.dfntList = dfntList;
		popupWindow = new PopupWindow(mContext);
		contentView = LayoutInflater.from(mContext).inflate(R.layout.video_definition_popupwindow, null);
		listView = (ListView) contentView.findViewById(R.id.video_definition_list);
		listView.setAdapter(new DefinitionAdapter());
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				VideoDefinitionPopupWindow.this.callBack.onItemclick(VideoDefinitionPopupWindow.this.dfntList.get(VideoDefinitionPopupWindow.this.dfntList.size() - 1 - position));
			}
		});

		popupWindow.setContentView(contentView);
		popupWindow.setWidth(ScreenUtil.dp2px(72));
		popupWindow.setHeight(this.dfntList.size() * ScreenUtil.dp2px(56));
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	/**
	 * 弹出popwindow
	 * 
	 * @param view
	 *            以这个布局的右下角为基准
	 * @param shareInfo
	 *            分享信息
	 */
	public void show(View view) {
		if (popupWindow != null) {
			popupWindow.showAsDropDown(view, view.getWidth() - ScreenUtil.dp2px(64), 0);
		}
	}

	/**
	 * 隐藏popwindow
	 */
	public void dismiss() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
		}
	}

	public interface ItemClickCallBack {
		void onItemclick(XyzplaRst.Dfnt dfnt);
	}

	private class DefinitionAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			if (dfntList == null) {
				return 0;
			}
			return dfntList.size();
		}

		@Override
		public Object getItem(int position) {
			if (dfntList == null || dfntList.size() == 0) {
				return null;
			} else {
				return dfntList.get(dfntList.size() - 1 - position);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			XyzplaRst.Dfnt dnft = dfntList.get(dfntList.size() - 1 - position);
			convertView = LayoutInflater.from(ApplicationManage.getGlobalContext()).inflate(R.layout.video_definition_item, null);
			ImageView logo = (ImageView) convertView.findViewById(R.id.definition_logo);
			switch (dnft.t) {
			case 1:
				// 标清
				if (dnft.cur == 1) {
					logo.setImageResource(R.drawable.full_icon_play_control_standard_choosed);
				} else {
					logo.setImageResource(R.drawable.video_fullscreen_standard_selector);
				}
				break;
			case 2:
				// 高清
				if (dnft.cur == 1) {
					logo.setImageResource(R.drawable.full_icon_play_control_high_choosed);
				} else {
					logo.setImageResource(R.drawable.video_fullscreen_high_selector);
				}
				break;
			case 3:
				// 超清
				if (dnft.cur == 1) {
					logo.setImageResource(R.drawable.full_icon_play_control_supper_choosed);
				} else {
					logo.setImageResource(R.drawable.video_fullscreen_supper_selector);
				}
				break;
			}
			return convertView;
		}
	}

	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	public void setDfntList(List<XyzplaRst.Dfnt> dfntList) {
		this.dfntList = dfntList;
	}

}
